//
//  TestDragViewCtrl.swift
//  TestZZBaseCustomsDemo
//
//  Created by Czz on 2023/1/17.
//

import UIKit
import ZZBase

class TestDragViewCtrl: UIViewController {

    override func viewDidLoad() {
        super.viewDidLoad()

//        self.img =[[UIImageView alloc] initWithImage:[UIImage imageNamed:@"pic4"]];
//        self.img.frame = CGRectMake(20, 40, CScreenWidth-40, 200);
//        self.img.userInteractionEnabled = YES;
//        [self.view addSubview:self.img];
//        //Drag发送数据，Drop接收数据
//        UIDragInteraction *dragInter = [[UIDragInteraction alloc] initWithDelegate:self];
//        dragInter.enabled = YES;
//        [self.img addInteraction:dragInter];
//        [self.view addInteraction:[[UIDropInteraction alloc] initWithDelegate:self]];
//
//        self.img2 =[[UIImageView alloc] init];
//        self.img2.frame = CGRectMake(20, CScreenHeight-250, CScreenWidth-40, 200);
//        self.img2.userInteractionEnabled = YES;
//        [self.view addSubview:self.img2];
        
        view.addSubview(img)
        
        let drag = UIDragInteraction(delegate: self)
        drag.isEnabled  = true
        img.addInteraction(drag)
        
        view.addInteraction(UIDragInteraction(delegate: self))
        
        view.addSubview(img2)
        
    }
    
    lazy var img: UIImageView = {
        let v = UIImageView(image: .zz_named("icon-72"))
            .zz_isUserInteractionEnabled(true)
            .zz_frame(CGRect(x: 20, y: 40, width: zz_screen_width - 40, height: 200))
        return v
    }()
    
    lazy var img2: UIImageView = {
        let v = UIImageView()
            .zz_isUserInteractionEnabled(true)
            .zz_frame(CGRect(x: 20, y: zz_screen_height - 250, width: zz_screen_width - 40, height: 200))
        return v
    }()
    

    /*
    // MARK: - Navigation

    // In a storyboard-based application, you will often want to do a little preparation before navigation
    override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
        // Get the new view controller using segue.destination.
        // Pass the selected object to the new view controller.
    }
    */

}

extension TestDragViewCtrl: UIDragInteractionDelegate{
    func dragInteraction(_ interaction: UIDragInteraction, itemsForBeginning session: UIDragSession) -> [UIDragItem] {
        img2.image = nil
        let item = NSItemProvider(object: img.image!)
        let dragItem = UIDragItem(itemProvider: item)
        dragItem.localObject = img.image
        return [dragItem]
    }
    
    func dragInteraction(_ interaction: UIDragInteraction, previewForLifting item: UIDragItem, session: UIDragSession) -> UITargetedDragPreview? {
        let parameters = UIDragPreviewParameters()
        parameters.visiblePath = UIBezierPath(roundedRect: img.bounds, cornerRadius: 20)
        let target = UIDragPreviewTarget(container: img.superview!, center: img.center)
        let dragPreview = UITargetedDragPreview(view: interaction.view!, parameters: parameters, target: target)
        return dragPreview
    }
    
    
    
    
    
}
